import 'package:flutter/material.dart';
import 'package:mi_mang_flutter/res/styles.dart';
import 'package:mi_mang_flutter/routers/fluro_navigator.dart';
import 'package:mi_mang_flutter/utils/dialog_util.dart';
import 'package:mi_mang_flutter/utils/locale_util.dart';

class BaseDialog extends StatefulWidget {
  BaseDialog({
    Key? key,
    required this.child,
    this.title = '',
    this.width = 270.0,
    this.onCancel,
    this.onPressed,
    this.hiddenButton = false,
  }) : super(key: key);

  /// 头部标题
  final String? title;

  /// 整体宽度
  final double width;

  /// 确定点击事件
  final Function? onCancel;

  /// 确定点击事件
  final Function? onPressed;

  /// 子控件
  final Widget child;

  /// 是否显示底部确定取消按钮(默认true)
  final bool hiddenButton;

  @override
  _BaseDialogState createState() => _BaseDialogState();
}

class _BaseDialogState extends State<BaseDialog> {
  final double _buttonHeight = 50.0;

  @override
  Widget build(BuildContext context) {
    BorderRadius borderRadius = BorderRadius.circular(8.0);
    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets +
          const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
      duration: const Duration(milliseconds: 120),
      curve: Curves.easeInCubic,
      child: MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: Center(
          child: Container(
            decoration: BoxDecoration(
              borderRadius: borderRadius,
              boxShadow: [
                BoxShadow(
                  color: Colors.black12,
                  offset: Offset(0.0, 2.0),
                  blurRadius: 10.0,
                  spreadRadius: -2.0,
                ),
              ],
            ),
            width: widget.width,
            child: Material(
              borderRadius: borderRadius,
              child: ClipRRect(
                borderRadius: borderRadius,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Offstage(
                      offstage: widget.title == '',
                      child: Padding(
                        padding: const EdgeInsets.only(top: 15.0),
                        child: Text(
                          widget.title!,
                          style: TextStyles.titleLarge,
                        ),
                      ),
                    ),
                    Flexible(child: widget.child),
                    // child,
                    Offstage(
                      offstage: widget.hiddenButton,
                      child: Divider(indent: 0.0),
                    ),
                    Offstage(
                      offstage: widget.hiddenButton,
                      child: _bottomButton(context),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _bottomButton(BuildContext context) {
    return Row(
      children: <Widget>[
        Expanded(
          child: MaterialButton(
            height: _buttonHeight,
            shape: RoundedRectangleBorder(),
            child: Text(
              i18n(context).cancel,
              style: TextStyles.labelLarge,
            ),
            onPressed: () {
              // NavigatorUtils.goBack(context);
              NavigatorUtils.goBackWithParams(context, false);
              if (widget.onCancel != null) {
                widget.onCancel!();
              }
            },
          ),
        ),
        Container(
          height: _buttonHeight,
          width: 0.5,
          color: Theme.of(context).dividerColor,
        ),
        Expanded(
          child: MaterialButton(
            height: _buttonHeight,
            shape: RoundedRectangleBorder(),
            child: Text(
              i18n(context).ok,
              style: TextStyles.labelLarge,
            ),
            onPressed: () {
              if (widget.onPressed != null) {
                widget.onPressed!();
              }
            },
          ),
        )
      ],
    );
  }

  @override
  void dispose() {
    DialogUtil.isShowDialog = false;
    super.dispose();
  }
}
